// -*-mode:c++; coding:utf-8-*-

#ifndef _KB_PERFORMANCE_MEASUREMENT_HPP_
#define _KB_PERFORMANCE_MEASUREMENT_HPP_

#include <iostream>

#include "xbase_utility.hpp"

#ifdef ENABLE_PERFORMANCE_MEASUREMENT
#	define MEASUREMENT_OF_TIME(name, id) ::kb::measurement_of_time __kb_measurement_of_time__(name, id)
#else
#	define MEASUREMENT_OF_TIME(name, id) (void)(0)
#endif

namespace kb
{

	struct measurement_of_time
	{
		measurement_of_time(const std::string &name,
				    uint64_t id,
				    std::ostream &out = std::cout)
			: m_out(out)
			, m_name(name)
			, m_id(id)
			, m_time_begin(cur_micro_seconds()) {}

		~measurement_of_time() {
			uint64_t time_end = cur_micro_seconds();
			m_out << '(' << m_name
			      << ' ' << m_id
			      << ' ' << m_time_begin
			      << ' ' << time_end
			      << ')' << std::endl;
		}

	private:
		std::ostream &m_out;
		const std::string m_name;
		uint64_t m_id;
		uint64_t m_time_begin;
	};

} // namespace kb

#endif	// _KB_PERFORMANCE_MEASUREMENT_HPP_
